﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using danreng.Enum;
using sharp_net;
using sharp_net.Repositories;
using sharp_net.Repositories.MongoDB;

namespace danreng.Domain {
    public class User : DomainMongo, IAggregateRoot {
        [Required]
        public string DisplayName { get; set; }

        public string Email { get; set; }

        [Required]
        [Column("Password")]
        [MaxLength(128)]
        public string PasswordHash { get; set; }
        public string Password {
            set { PasswordHash = EncryptPassword(value); }
        }
        public bool EqualsPassword(string value) {
            return this.PasswordHash.Equals(EncryptPassword(value));
        }
        public static string EncryptPassword(string value) {
            return Encrypt.EncryptUserPassword(value);
        }

        public int HostEnum { get; set; }
        public eUserHost Host {
            get { return (eUserHost)HostEnum; }
            set { HostEnum = (int)value; }
        }
        public string HostKey { get; set; }

        public DateTime LastSyncTime { get; set; }

        public void Update(UserData data) {
            base.Update((DomainMongoData)data);
            if (!string.IsNullOrEmpty(data.DisplayName))
                this.DisplayName = data.DisplayName;
            if (data.HostEnum != 0)
                this.HostEnum = data.HostEnum;
            if (!string.IsNullOrEmpty(data.HostKey))
                this.HostKey = data.HostKey;
            if (data.LastSyncTime != null)
                this.LastSyncTime = data.LastSyncTime;
        }
    }

    public class UserData : DomainMongoData {
        public string DisplayName { get; set; }
        public string Email { get; set; }
        public string PasswordHash { get; set; }
        public int HostEnum { get; set; }
        public string HostKey { get; set; }
        public DateTime LastSyncTime { get; set; }
    }
}
